package com.bdqn.servlet.admin;

import com.bdqn.entity.ResultInfo;
import com.bdqn.service.AdminService;
import com.bdqn.service.Impl.AdminServiceImpl;
import com.bdqn.util.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "AddWayServlet",urlPatterns = "/addWay")
public class AddWayServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String strid = request.getParameter("id");
        int id = 0;
        if(strid !=null && strid.length()>0){
            id=Integer.parseInt(strid);
        }
        String end = request.getParameter("end");
        String start = request.getParameter("start");
        String strweek = request.getParameter("week");
        String strdepId = request.getParameter("depId");
        int depId = 0;
        if(strdepId!=null && strdepId.length()>0){
            depId = Integer.parseInt(strdepId);
        }
        int week = 0;
        if(strweek!=null && strweek.length()>0){
            week=Integer.parseInt(strweek);
        }
        boolean flag ;
        ResultInfo info = new ResultInfo();
        AdminService service = new AdminServiceImpl();

        if(id==1){
            flag = service.update(week,depId,start,end);
            if(!flag){
                info.setErrorMsg("当日还没有设置考勤规则！请先设置考勤规则！");
            }
        }else{
            service = new AdminServiceImpl();
            flag = service.addWay(week,depId,start,end);
            if(!flag){
                info.setErrorMsg("当日已存在规则，请前往修改页面进行修改！");
            }
        }
        info.setFlag(flag);
        JSON.writeAsString(info,response);
    }
}
